/*
 * cng.c
 *
 *  Created on: Sep 20, 2013
 *      Author: Ken Arok
 */


#include "config_apps.h"

#if APPLICATION_CNG

#include <string.h>
#include "timers_hw.h"
#if DEBUG_CNG
#include "rtc.h"
#include "display.h"
#endif
#include "inter_chip.h"
#include "wiznet.h"
#include "cng.h"


#define ADVANZ_MSG_STX		0x01
#define ADVANZ_MSG_ETX		0x02

//! Command with argument
#define CNG_TURN_DISPLAY_ALARM_ON		0x81
#define CNG_TURN_DISPLAY_ALARM_OFF		0x82
#define CNG_TURN_DISPLAY_ALARM_BLINK	0x83

#if DEBUG_CNG
static char _DisplayMsg[20];
static rtc_string_t _rtc;

/** \brief Update Home Screen.
 *
 */
static void prvCNG_HomeScreen(void);

#endif /* DEBUG_CNG */

/** \brief Parse Message from Advanz.
 *
 * \param _pBuf		Pointer to message.
 *
 * \return 0 if frame is correct. Unless return > 0;
 */
static uint8_t prvCNG_CheckAdvanzDisplayAlarmMessage(void *_pBuf);

/* -------------------------------------------------------------------------------------- */

void vStartCGN_Apps(void)
{
#if DEBUG_CNG
	volatile uint32_t *_tmrUpdateScreen = NULL;
#endif
	uint32_t _msgLen;
	char *_end = NULL, *_msgString, _pBuf[50], _str[3];
	uint8_t _Response;
	uint8_t _advanzCmd;

#if DEBUG_CNG
	/* Obtain Timer for Update Screen. */
	_tmrUpdateScreen = vTimer500msTake();
	if(_tmrUpdateScreen == NULL) {
		vClearDisplay();
		vPrintDisplay(1, 1, "Fatal Error: 0xFE");
		while(1);
	}
	*_tmrUpdateScreen = 2;
#endif /* DEBUG_CNG */

	/* Initialize Wiznet Communication. */
	vWiznetInit();


	/* Start listen message from Wiznet. */
	vWiznetStartListen();

	for(;;) {
		if(vWiznetIsMessageReceived() == true) {

		#if DEBUG_CNG
			vTurnBackLightDisplay(iON, 0);
			vTurnBackLightDisplay(iOFF, 25);
		#endif /* DEBUG_CNG */

			_msgString = vWiznetReceivedMessage();
			_Response = prvCNG_CheckAdvanzDisplayAlarmMessage((void *)_msgString);

			if(!_Response) {
				_msgString = _msgString + 38;

				/* For Display properties. */
				_msgLen = strlen((const char *)_msgString);
				if(_msgLen > 16) {
					_msgLen =  16;
				}

				/* Parse Advanz Message */
				_str[0] = _msgString[0];
				_str[1] = _msgString[1];
				_str[2] = '\0';
				_advanzCmd = (uint8_t)strtoul((const char *)_str, &_end, 10);
				/* Inquiry to MEGA162. */
				vADO_Polling(_advanzCmd | 0x80, (void *)_pBuf, 40);

				if(_pBuf[0] == ADO_PROTOCOL_ACK) {
					/* Send Index and Argument to MEGA162. */
					_Response = vADO_Selecting((const void *)&_msgString[2], 12);
					if(ADO_PROTOCOL_ACK == _Response) {
						/* End of transmission. */
						vADO_EndOfTransmission();
					#if DEBUG_CNG
						_DisplayMsg[_msgLen + 2] = 'A';
					#endif
					}
				#if DEBUG_CNG
					else if(ADO_PROTOCOL_EOT == _Response) {
						_DisplayMsg[_msgLen + 2] = 'E';
					}
					else {
						_DisplayMsg[_msgLen + 2] = 'N';
					}
				#endif /* DEBUG_CNG */
				}

			#if DEBUG_CNG
				/* Show on display. */
				memcpy((void *)_DisplayMsg, (const void *)_msgString, _msgLen + 1);
				_DisplayMsg[_msgLen] = '-';
				_DisplayMsg[_msgLen + 1] = _pBuf[0] + 0x30;
				_DisplayMsg[_msgLen + 3] = '\0';
			#endif /* DEBUG_CNG */
			}
		#if DEBUG_CNG
			else {
				_DisplayMsg[0] = _Response + 0x30;
				_DisplayMsg[1] = '\0';
			}
		#endif /* DEBUG_CNG */

			vWiznetStartListen();
		}

	#if DEBUG_CNG
		if(*_tmrUpdateScreen == 0) {
			*_tmrUpdateScreen = 1;
			prvCNG_HomeScreen();
		}
	#endif /* DEBUG_CNG */
	}
}


/* --------------------------------------------------------------------------------- */

#if DEBUG_CNG

static void prvCNG_HomeScreen(void)
{
	const char _SoftName[] = "CNG v1.0";

	/* Read RTC. */
	vReadRTC(&_rtc);
	/* Update Screen. */
	vClearDisplay();
	vPrintDisplay(1, 1, _SoftName);
	vPrintDisplay(2, 1, (const char *)_DisplayMsg);
	vPrintDisplay(3, 1, _rtc.time);
	vPrintDisplay(4, 1, _rtc.date1);
}

#endif /* DEBUG_CNG */

static uint8_t prvCNG_CheckAdvanzDisplayAlarmMessage(void *_pBuf)
{
	uint8_t *_ptr = _pBuf;

	if(_ptr[0] != ADVANZ_MSG_STX) return 1;

	if((_ptr[35] != 'L') || (_ptr[36] != '0') || (_ptr[37] != '1')) return 2;

	return 0;
}




#endif /* APPLICATION_CNG */
